package com.link.base.base.billstatementparam.controller;

import com.link.base.base.enterprise.model.Enterprise;
import com.link.base.user.model.CoreUser;
import com.link.base.base.billstatementparam.model.BillStataementModel;
import com.link.base.base.billstatementparam.model.BillStatementParam;
import com.link.base.base.billstatementparam.service.BillStatementParamService;
import com.link.base.base.enterprise.dao.mybatis.mapper.EnterpriseMapper;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.model.BasicModel;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.BasicServiceException;
import com.link.core.basic.service.ServiceException;
import com.link.core.util.DateUtil;
import com.link.core.util.UserUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 参数管理 对账单参数controller
 *
 * @author crm4
 * @version 1.0
 * @date: 2018-06-22
 */
@Controller
@RequestMapping("/link/billStatementParam")
public class BillStatementParamController extends BasicController<BillStatementParam> {

    @Resource
    private BillStatementParamService billStatementParamService;

    @Resource
    private EnterpriseMapper enterpriseMapper;

    @Override
    public BasicService<BillStatementParam> getBasicService() throws Exception {
        return billStatementParamService;
    }

    /**
     * 分页查询
     *
     * @param qps
     * @param entity
     * @param session
     * @param request
     * @param response
     * @return
     */
    @Override
    @RequestMapping(value = "/queryByExamplePage")
    @ResponseBody
    public Map<String, Object> queryByExamplePage(@JsonParam QueryParams qps, @JsonParam BillStatementParam entity, HttpSession session,
                                                  HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            qps.preDealSecurity(request);
            BillStatementParam t = (BillStatementParam) BasicModel.transformClass(entity, qps);
            beforQueryAllOrExample(t, request);
            beforQueryExample(t, request);
            //当前登陆用户对应的账套
            CoreUser user = UserUtil.getUser();
            //查询当前账套下的企业数据
            Enterprise et = new Enterprise();
            et.setCode(user.getLoginCorpId());
            et.setPage(1);
            et.setPageFlag(false);
            et.setTotalFlag(false);
            List<Enterprise> enterprises = enterpriseMapper.queryByExamplePage(et);
            if (enterprises.isEmpty()) {
                throw new ServiceException("PUBLIC-042");
            }
            //查询当前账套下的对账单参数
            List<BillStatementParam> list = getBasicService().queryByExamplePage(t);
            BillStataementModel bsm = new BillStataementModel();
            bsm.setEnterpriseId(enterprises.get(0).getId());
            bsm.setBillStatementFlag(StringUtils.isEmpty(enterprises.get(0).getBillStatementFlag())
                    ? "N" : enterprises.get(0).getBillStatementFlag());
            bsm.setBillStatementParamList(list);
            result.put("success", true);
            result.put("rows", bsm.getBillStatementParamList());
            result.put("billStatementFlag", bsm.getBillStatementFlag());
            result.put("enterpriseId", bsm.getEnterpriseId());
            result.put("total", getCount((BasicModel) t, list.size()));
        } catch (BasicServiceException e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("rows", new ArrayList<BillStataementModel>());
            result.put("result", e.getMessage());
            result.put("detailMessage", e.getDetailMessage());
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("rows", new ArrayList<BillStataementModel>());
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 更新前操作
     *
     * @param entity
     * @param request
     * @throws Exception
     */
    @Override
    public void beforUpsert(BillStatementParam entity, HttpServletRequest request) throws Exception {
        if (null == entity.getPeriodId() || StringUtils.isEmpty(entity.getPreRunStartDate())) {
            throw new ServiceException("PUBLIC-015");
        }
    }

    /**
     * 生成指定对账单
     *
     * @param bdm
     * @return
     */

    @RequestMapping(value = "/batchCreateBillAccnt", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> batchCreateBillAccnt(@JsonParam BillStatementParam bdm) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            //企业对单开关
            bdm.setAttr1("Y");
            bdm.setCloseFlag("N");
            bdm.setPreRunStartDate(DateUtil.dateToStr(new Date(), "yyyy-MM-dd"));
            bdm.setPageFlag(false);
            billStatementParamService.billAccntCreateALL(bdm);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }
}
